package com.serviceprovider.business.module.admin.action.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.action.model.AclActionApply;
import com.serviceprovider.business.module.admin.action.services.dao.interfaces.IActionApplyService;
import com.serviceprovider.business.utility.AppUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Service;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("actionApplyService")
public class ActionApplyServiceImpl extends JdbcDaoSupport implements IActionApplyService {
    @Autowired
    public ActionApplyServiceImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public List<AclActionApply> findAll() {
        String               sql  = "select id,class_id,role_id,action_id,flag from acl_action_apply";    // $NON-NLS-1$
        List                 list = getJdbcTemplate().queryForList(sql);
        List<AclActionApply> ret  = new ArrayList<AclActionApply>();
        Map                  row  = null;
        AclActionApply       aux  = null;

        for (Object obj : list) {
            row = (Map) obj;
            aux = new AclActionApply();
            aux.setId(new Integer(String.valueOf(row.get("id"))));    // $NON-NLS-1$
            aux.setClassId(new Integer(String.valueOf(row.get("class_id"))));
            aux.setRoleId(new Integer(String.valueOf(row.get("role_id"))));
            aux.setActionId(new Integer(String.valueOf(row.get("action_id"))));
            aux.setFlag(AppUtil.getBoolean(String.valueOf(row.get("flag"))));
            ret.add(aux);
        }

        return ret;
    }

    @Override
    public void insertAclActionApply(AclActionApply l) {
        final String sql = "insert into acl_action_apply (class_id,role_id,action_id,flag) values(?,?,?,?)";    // $NON-NLS-1$

        getJdbcTemplate().update(sql, new Object[] { l.getClassId(), l.getRoleId(), l.getActionId(), l.isFlag() });
    }

    @Override
    public void updateAclActionApply(AclActionApply l) {
        final String sql =
            "update acl_action_apply set class_id = ? , role_id = ?, action_id = ?  , flag = ? where class_id = ? and role_id = ? and action_id = ?";    // $NON-NLS-1$

        getJdbcTemplate().update(sql, new Object[] {
            l.getClassId(), l.getRoleId(), l.getActionId(), l.isFlag(), l.getClassId(), l.getRoleId(), l.getActionId()
        });
    }

    protected boolean isActionApplied(int classId, int roleId, int actionId) {
        final String sql = "select count(*) from acl_action_apply where class_id = ? and role_id = ? and action_id = ?";

        return getJdbcTemplate().queryForInt(sql, new Object[] { classId, roleId, actionId }) > 0;
    }

    @Override
    public void save(List<AclActionApply> list) {
        for (AclActionApply l : list) {
            if (!isActionApplied(l.getClassId(), l.getRoleId(), l.getActionId())) {    // we expect id = 0 for new records
                insertAclActionApply(l);
            } else {
                updateAclActionApply(l);
            }
        }
    }
}



